﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace EzDeploy.Core.Design.Editor
{
    internal class AssemblyFileNameEditor : UITypeEditor
    {
        OpenFileDialog _openFileDialog;

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                IWindowsFormsEditorService windowsFormsEditorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (windowsFormsEditorService != null)
                {
                    if (this._openFileDialog == null)
                    {
                        this._openFileDialog = new OpenFileDialog();
                        this.InitializeDialog(this._openFileDialog);
                    }
                    if (value is string)
                    {
                        this._openFileDialog.FileName = (string)value;
                    }
                    if (this._openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        value = this._openFileDialog.FileName;
                    }
                }
            }
            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        protected virtual void InitializeDialog(OpenFileDialog openFileDialog)
        {
            openFileDialog.Filter = "Executable|*.exe|DLL|*.dll";
            openFileDialog.Title = "Browse Assembly.";
        }
    }
}